package com.backend.mzyf.Utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UploadUtil {

    private static final String BOOT_PATH = "/upload/";

    public static Boolean saveFile(MultipartFile file) {
        // 判断文件是否为空
        if (!file.isEmpty()) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Long timeStamp = new Date().getTime();
            String[] fileName = file.getOriginalFilename().split("\\.");
            System.out.println(fileName);
            String path = BOOT_PATH+format.format(timeStamp)+"/";
            try {
                File filepath = new File(path);
                if (!filepath.exists())
                    filepath.mkdirs();
                // 文件保存路径
                String savePath = path + timeStamp + "."+ fileName[1];
                // 转存文件
                file.transferTo(new File(savePath));
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
